package oa.hleast.xswl.service.activity.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import oa.hleast.common.web.exception.BizException;
import oa.hleast.xswl.mapper.activity.CmsActivityAttributesMapper;
import oa.hleast.xswl.pojo.domain.activity.CmsActivityAttributes;
import oa.hleast.xswl.service.activity.ICmsActivityAttributesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
@AllArgsConstructor
public class CmsActivityAttributesServiceImpl extends ServiceImpl<CmsActivityAttributesMapper, CmsActivityAttributes> implements ICmsActivityAttributesService {
    @Override
    public List<CmsActivityAttributes> findAttributeByTypeId(Long activityTypeId) {
        LambdaQueryWrapper<CmsActivityAttributes> queryWrapper = new LambdaQueryWrapper<CmsActivityAttributes>().eq(CmsActivityAttributes::getActivityTypeId, activityTypeId).orderByDesc(CmsActivityAttributes::getId);
        return this.list(queryWrapper);
    }

    @Override
    public Boolean attributeAdd(List<CmsActivityAttributes> formList) {

        if (CollectionUtil.isEmpty(formList)) {
            throw new BizException("至少提交一条属性");
        }

        Long typeId = formList.get(0).getActivityTypeId();

        List<Long> formIds = formList.stream().map(item -> item.getId()).collect(Collectors.toList());

        List<Long> databaseIds = this
                .list(new LambdaQueryWrapper<CmsActivityAttributes>()
                        .eq(CmsActivityAttributes::getActivityTypeId, typeId)
                        .select(CmsActivityAttributes::getId)
                ).stream()
                .map(item -> item.getId())
                .collect(Collectors.toList());

        // 删除的商品分类属性
        if (CollectionUtil.isNotEmpty(databaseIds)) {
            List<Long> removeIds = databaseIds.stream()
                    .filter(id -> CollectionUtil.isEmpty(formIds) || !formIds.contains(id))
                    .collect(Collectors.toList());
            if (CollectionUtil.isNotEmpty(removeIds)) {
                this.removeByIds(removeIds);
            }
        }

        boolean result = this.saveOrUpdateBatch(formList);

        return result;
    }

    @Override
    public boolean add(CmsActivityAttributes form) {
        return this.save(form);
    }

    @Override
    public boolean update(CmsActivityAttributes form) {
        return this.updateById(form);
    }

    @Override
    public Page<CmsActivityAttributes> pageList(Integer page, Integer limit, Long typeId, Boolean display) {
        LambdaQueryWrapper<CmsActivityAttributes> queryWrapper = new LambdaQueryWrapper<CmsActivityAttributes>().eq(CmsActivityAttributes::getActivityTypeId, typeId);

        Page<CmsActivityAttributes> attributesPage = this.page(new Page<>(page, limit), queryWrapper);

        return attributesPage;
    }

    @Override
    public boolean deleteByIds(List<String> asList) {
        return this.removeByIds(asList);
    }

    @Override
    public CmsActivityAttributes detail(Long id) {
        return this.getById(id);
    }
}
